<template>
    <div>
        <!-- //警告 -->
        <el-alert title="注意：只允许为第三级分类设置参数!" type="warning" show-icon>
        </el-alert>
        <!-- 选择 -->
        <div style="margin: 20px 0px;">
            选择商品分类:<el-cascader v-model="value" :options="options"
                :props="{ expandTrigger: 'hover', label: 'cat_name', value: 'cat_id' }" @change="select"></el-cascader>
        </div>
        <!-- //tab -->
        <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="动态参数" name="many">
                <el-button type="primary" :disabled="!value" @click="addDialogVisible = true">添加参数</el-button>
                <el-table :data="DynamicList" style="width: 100%">
                    <el-table-column type="expand">
                        <template slot-scope="props">
                            <el-tag :key="tag" v-for="(tag, i) in props.row.attr_vals" closable
                                :disable-transitions="false" @close="handleClose(props.row, i)" size="small">
                                {{  tag  }}
                            </el-tag>
                            <el-input class="input-new-tag" v-if="props.row.inputVisible" v-model="props.row.inputValue"
                                ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm(props.row)">
                            </el-input>
                            <el-button v-else class="button-new-tag" size="small" @click="showInput(props.row)">点击添加
                            </el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="#" type="index">
                    </el-table-column>

                    <el-table-column label="参数名称" prop="attr_name">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="props">
                            <el-button type="primary" icon="el-icon-edit" @click="edit(props.row)">编辑</el-button>
                            <el-button type="danger" icon="el-icon-delete" @click="del(props.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>
            <el-tab-pane label="静态属性" name="only">

                <el-button type="primary" :disabled="!value" @click="addDialogVisible = true">添加属性</el-button>
                <el-table :data="StaticList" style="width: 100%">
                    <el-table-column type="expand">
                        <template slot-scope="props">
                            <el-tag :key="tag" v-for="(tag, i) in props.row.attr_vals" closable
                                :disable-transitions="false" @close="handleClose(props.row, i)" size="small">
                                {{  tag  }}
                            </el-tag>
                            <el-input class="input-new-tag" v-if="props.row.inputVisible" v-model="props.row.inputValue"
                                ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm(props.row)">
                            </el-input>
                            <el-button v-else class="button-new-tag" size="small" @click="showInput(props.row)">点击添加
                            </el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="#" type="index">
                    </el-table-column>

                    <el-table-column label="参数名称" prop="attr_name">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="props">
                            <el-button type="primary" icon="el-icon-edit" @click="edit(props.row)">编辑</el-button>
                            <el-button type="danger" icon="el-icon-delete" @click="del(props.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

            </el-tab-pane>
        </el-tabs>
        <!-- 添加弹出框 -->
        <el-dialog :title="activeName == 'many' ? '添加动态参数' : '添加静态属性'" :visible.sync="addDialogVisible" width="30%"
            :before-close="handleClose">
            {{  activeName == 'many' ? '添加动态参数' : '添加静态属性'  }}
            <el-input v-model="input"></el-input>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="ensure">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog :title="activeName == 'many' ? '修改动态参数' : '修改静态属性'" :visible.sync="editDialogVisible" width="30%"
            :before-close="handleClose">
            {{  activeName == 'many' ? '修改动态参数' : '修改静态属性'  }}
            <el-input v-model="input"></el-input>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editEnsure">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { categories, cut, add, editcanshu, dels } from '@/http/goods'
import Vue from 'vue';
export default {
    data() {
        return {
            //选中的分类id
            value: '',
            //分类参数
            options: [],
            //请求方式
            activeName: 'many',
            //动态列表
            DynamicList: [],
            //静态列表
            StaticList: [],
            //添加input
            input: '',
            //添加弹出框状态
            addDialogVisible: false,
            //编辑弹出框状态
            editDialogVisible: false,
            //编辑的id
            id: 0
        }
    },
    computed: {

    },
    methods: {
        //渲染
        async select() {
            let res = await cut(this.value[this.value.length - 1], { sel: this.activeName })
            console.log(res, 3);
            res.data.forEach(element => {
                if (!element.attr_vals) {
                    element.attr_vals = []
                    element.inputVisible = false
                    element.inputValue = ''
                    return false
                }
                element.attr_vals = element.attr_vals.split(',')
                element.inputVisible = false
                element.inputValue = ''
            });
            if (this.activeName == 'many') {
                this.DynamicList = res.data
            } else {
                this.StaticList = res.data
            }
        },
        //tab切换的时候请求
        handleClick() {
            if (!this.value) {
                return
            }
            this.select()
        },
        //标签删除
        async handleClose(row, i) {

            row.attr_vals.splice(i, 1)
            let res = await editcanshu(this.value[this.value.length - 1], row.attr_id,
                { attr_name: row.attr_name, attr_sel: this.activeName, attr_vals: row.attr_vals.join(',') })


        },
        //回车
        async handleInputConfirm(row) {
            row.attr_vals.push(row.inputValue)
            let res = await editcanshu(this.value[this.value.length - 1], row.attr_id,
                { attr_name: row.attr_name, attr_sel: this.activeName, attr_vals: row.attr_vals.join(',') })
            row.inputValue = ''
            row.inputVisible = false
        },
        //点击输入框出来
        showInput(row) {
            row.inputVisible = true
        },
        //添加
        async ensure() {
            let res = await add(this.value[this.value.length - 1],
                { attr_name: this.input, attr_sel: this.activeName })
            this.select()
            this.addDialogVisible = false
            this.input = ''
        },
        //edit编辑回填
        edit(row) {
            console.log(row);
            this.input = row.attr_name
            this.editDialogVisible = true
            this.id = row.attr_id
        },
        //编辑
        async editEnsure() {
            let res = await editcanshu(this.value[this.value.length - 1], this.id,
                { attr_name: this.input, attr_sel: this.activeName })

            this.editDialogVisible = false
            this.select()
            this.input = ''
        },
        //删除参数
        async del(row) {
            let res = await dels(this.value[this.value.length - 1], row.attr_id)
            this.select()
        }



    },
    created() {
        categories().then(res => {
            this.options = res.data
        })
    }
}
</script>

<style >
.el-cascader-panel {
    height: 200px;
}

.el-tag {
    margin: 10px;
}

.input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
}
</style>